package com.haulmont.asu.web.ui.department;

import com.haulmont.asu.entity.Department;
import com.haulmont.asu.entity.Work;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.actions.CreateAction;
import com.haulmont.cuba.gui.components.actions.EditAction;

import javax.inject.Inject;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Author: Sergey42
 * Date: 16.10.13 23:18
 */
public class DepartmentBrowser extends AbstractLookup {

    @Inject
    protected Table departmentsTable;

    @Inject
    protected DateField df;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        departmentsTable.removeAction(getAction("departmentsTable.create"));
        departmentsTable.addAction(new CreateAction(departmentsTable, WindowManager.OpenType.DIALOG));

        departmentsTable.removeAction(getAction("departmentsTable.create"));
        departmentsTable.addAction(new EditAction(departmentsTable, WindowManager.OpenType.DIALOG));
    }

    public void checkLoad() {
        if (df.getValue() == null) {
            return;
        }

        if (departmentsTable.getSelected().size() != 1) {
            return;
        }

        Department department = departmentsTable.getSingleSelected();

        LoadContext lc = new LoadContext(Work.class);
        lc.setQuery(
                new LoadContext.Query("select w from asu$Work w where w.department.id = :id")
                        .setParameter("id", department.getId()));

        List<Work> works = getDsContext().getDataSupplier().loadList(lc);

        if (works.isEmpty()) {
            showNotification(getMessage("empty"), NotificationType.HUMANIZED);
            return;
        }

        List<Work> active = new ArrayList<>();

        Date checkDate = df.getValue();

        for (Work work : works) {
            if (work.getPlanStartDate() != null && work.getPlanFinishDate() != null) {
                if (checkDate.after(work.getPlanStartDate()) && checkDate.before(work.getPlanFinishDate())) {
                    active.add(work);
                }
            }
        }

        if (active.isEmpty()) {
            showNotification(getMessage("empty"), NotificationType.HUMANIZED);
            return;
        }

        BigDecimal power = new BigDecimal(department.getStuffCount() * 100);
        BigDecimal task = new BigDecimal(0);
        for (Work work : active) {
            task = task.add(work.getAmount());
        }

        String taskStr = task.toString();
        if (taskStr.contains(".")) {
            taskStr = taskStr.substring(0, taskStr.indexOf("."));
        }

        showOptionDialog(
                formatMessage("title", department.getName()),
                formatMessage("mess",
                        new SimpleDateFormat("dd.MM.yyyy").format(checkDate),
                        active.size(),
                        power,
                        taskStr),
                MessageType.CONFIRMATION_HTML,
                new Action[]{
                        new AbstractAction("OK") {
                            @Override
                            public void actionPerform(Component component) {
                            }
                        }
                }
        );

    }
}
